package com.lovo.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.stock.bean.PurcartBean;
import com.lovo.stock.bean.UserBean;
import com.lovo.stock.mapper.PurcartMapper;
import com.lovo.stock.service.IGoodService;
import com.lovo.stock.service.IPurcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurcartServiceImpl extends ServiceImpl<PurcartMapper, PurcartBean> implements IPurcartService {
    @Autowired
    PurcartMapper purcartMapper;

    @Override
    public List<PurcartBean> queryPurcartByUserId(UserBean userBean) {
        QueryWrapper q=new QueryWrapper<>();
        q.eq("purcart_user_id",userBean.getUserId());
        return purcartMapper.selectList(q);
    }

    @Override
    public void addPurcart(PurcartBean purcartBean) {
        //判断购物车内有无该商品
        QueryWrapper q=new QueryWrapper<>();
        q.eq("purcart_good_num",purcartBean.getPurcartGoodNum());
        PurcartBean purcart=purcartMapper.selectOne(q);
        //如果没有该商品,则加入购物车
        if (purcart==null){
            purcartMapper.insert(purcartBean);
        }else {
            //有该商品,采购数加1
            purcart.setPurcartNumber(purcart.getPurcartNumber()+1);
            purcartMapper.updateById(purcart);
        }

    }

    @Override
    public void deletePurcartById(int id) {
        purcartMapper.deleteById(id);
    }

    @Override
    public void deletePurcartByUserId(UserBean userBean) {
        QueryWrapper q=new QueryWrapper<>();
        q.eq("purcart_user_id",userBean.getUserId());
        purcartMapper.delete(q);
    }

    @Override
    public void updatePucart(PurcartBean purcartBean) {
        purcartMapper.updateById(purcartBean);
    }
}
